package com.hdey.service.heng.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdey.mapper.heng.UserMapper;
import com.hdey.pojo.heng.TbUscUser;
import com.hdey.service.heng.UserService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;

@Service("userService")
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, TbUscUser> implements UserService {
    @Resource
    private UserMapper mapper;

    @Override
    public Page<TbUscUser> showAll(Integer current, Integer size, String userName, Integer roleId) {
        System.out.println(roleId);
        Page<TbUscUser> page=new Page<>(current,size);
        LambdaQueryWrapper<TbUscUser> wrapper=new LambdaQueryWrapper<>();

        if(userName!=null&&!"".equals(userName)&&!"null".equals(userName)){
            wrapper.like(TbUscUser::getUserName,userName);
        }
        if(roleId!=0&&!"".equals(roleId)&&!"null".equals(roleId)&&roleId!=null){
            wrapper.eq(TbUscUser::getRoleId,roleId);
        }
        mapper.selectPage(page, wrapper);
        return page;
    }

    @Override
    public TbUscUser admin(String loginName) {
        LambdaQueryWrapper<TbUscUser> wrapper= Wrappers.lambdaQuery();
        wrapper.eq(TbUscUser::getLoginName,loginName);
        return mapper.selectOne(wrapper);
    }

    @Override
    public int add(TbUscUser tbUscUser) {
        tbUscUser.setCreateBy("张三");
        tbUscUser.setCreateDate(LocalDate.now());
        return mapper.insert(tbUscUser);
    }

    @Override
    public TbUscUser showByloginName(String loginName) {
        return mapper.showByloginName(loginName);
    }

    @Override
    public int del(Integer userId) {
        return mapper.deleteById(userId);
    }

    @Override
    public TbUscUser showByUserId(Integer userId) {
        return mapper.selectById(userId);
    }

    @Override
    public int update(TbUscUser tbUscUser) {
        return mapper.updateById(tbUscUser);
    }

    @Override
    public Integer selectCoutUser(Integer roleId) {
        return mapper.selectCoutUser(roleId);
    }
}
